package com.uenpay.im.ui.old.activity.attendance;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.weight.utils.baidumap.BaiDuMapUtil;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.ui.old.activity.map.LocationActivtiy;
import com.uenpay.im.callback.IDateDailogListener;
import com.uenpay.im.weight.MyDateDialog;
import com.uenpay.util.app.base.BaseActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * @author SL
 * @created 2016-3-21 13:00:12
 * @copyRight http://www.qiyunxin.com
 */
public class SetSignInTimeActivity extends BaseActivity {

    private TextView startTimeTv, endTimeTv, addressTv;
    private int entid;
    private String latitude = "";
    private String longitude = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_setting_attendance_layout;
    }

    @Override
    public void initView(View view) {
        entid = getIntent().getIntExtra("entid", -1);
        initView();
        initListener();
        getTimeLable();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.setting_attendance);
        addressTv = (TextView) findViewById(R.id.attendance_address);
        endTimeTv = (TextView) findViewById(R.id.attendance_end_time);
        startTimeTv = (TextView) findViewById(R.id.attendance_start_time);
    }

    protected void initListener() {
        findViewById(R.id.attendance_address_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        ActivityHandle.startActivity(
                                SetSignInTimeActivity.this,
                                LocationActivtiy.class, null, 100);
                    }
                });
        findViewById(R.id.attendance_start_time_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        setMovementDate(startTimeTv);
                    }
                });
        findViewById(R.id.attendance_end_time_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        setMovementDate(endTimeTv);
                    }
                });
        findViewById(R.id.submit_bt).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String start = startTimeTv.getText().toString();
                String end = endTimeTv.getText().toString();
                String address = addressTv.getText().toString();
                if (!TextUtils.isEmpty(start) && !TextUtils.isEmpty(end)
                        && !TextUtils.isEmpty(address)) {
                    setTimeLable(start, end);
                }
            }
        });
    }

    private void setMovementDate(final TextView textview) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm",
                Locale.getDefault());
        int selectedHour = -1;
        int selectedMinute = -1;
        try {
            if (!TextUtils.isEmpty(textview.getText().toString())) {
                Calendar calendar = Calendar.getInstance();
                String str = String.valueOf(sdf.parse(
                        textview.getText().toString()).getTime());
                calendar.setTimeInMillis(Long.valueOf(str));
                selectedHour = calendar.get(Calendar.HOUR_OF_DAY);
                selectedMinute = calendar.get(Calendar.MINUTE);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        MyDateDialog dateDialog = new MyDateDialog(-1, -1, -1, selectedHour,
                selectedMinute, SetSignInTimeActivity.this, true, false,
                new IDateDailogListener() {

                    @Override
                    public void OnDailogChick(String year, String Month,
                                              String Day, String hour, String minute) {
                        textview.setText(hour + ":" + minute);
                    }
                });
        dateDialog.showDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK) {
            double[] la_lg = BaiDuMapUtil.baiduToGaode(
                    data.getDoubleExtra("latitude", 0),
                    data.getDoubleExtra("longitude", 0));
            latitude = la_lg[0] + "";
            longitude = la_lg[1] + "";
            String address = data.getStringExtra("address");
            if (!TextUtils.isEmpty(address)) {
                addressTv.setText(address);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    AttendanceHandle mAttendanceHandle = new AttendanceHandle();

    private void setTimeLable(String startTime, String endTime) {
        mAttendanceHandle.setTimeLable(entid, startTime, endTime,
                latitude + "", longitude + "", new AttendanceHandle.IAttendanceResultListener() {

                    @Override
                    public void onResult(int status, String msg) {
                        if (!TextUtils.isEmpty(msg)) {
                            IMApplication.showToast(msg);
                        }
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            finish();
                        }
                    }
                });
    }

    private void getTimeLable() {
        mAttendanceHandle.getSignInTime(entid, new AttendanceHandle.IGetSignInTimeListener() {

            @Override
            public void onGetSignInResult(int status, String msg,
                                          String address, String sign_in_time, String sign_out_time,
                                          String _latitude, String _longitude) {
                if (!TextUtils.isEmpty(_latitude)
                        && !TextUtils.isEmpty(_longitude)) {
                    latitude = _latitude;
                    longitude = _longitude;
                }
                if (!TextUtils.isEmpty(address)) {
                    addressTv.setText(address);
                }
                if (!TextUtils.isEmpty(sign_in_time)) {
                    startTimeTv.setText(sign_in_time);
                }
                if (!TextUtils.isEmpty(sign_out_time)) {
                    endTimeTv.setText(sign_out_time);
                }
            }
        });
    }
}
